package com.dawnClass.controller;

import com.dawnClass.pojo.Consumer;
import com.dawnClass.service.LearningRateService;
import com.dawnClass.service.MycourseService;
import com.dawnClass.service.VideoService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class VideoController {

    @Autowired
    private MycourseService mycourseService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private LearningRateService learningRateService;

    @GetMapping("/video/{videoId}")
    public ModelAndView toVideo(@PathVariable("videoId") Integer videoId , HttpSession session){
        //获取当前登录用户
        ServiceResponse serviceResponse = (ServiceResponse) session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer) serviceResponse.getdata();

        ModelAndView mav = new ModelAndView();

        //获取用户课程信息
        ServiceResponse consumerCourse = mycourseService.ifBuyConsumer(consumer.getConsumerId(), videoId);
        if (consumerCourse.getCode() == 201){
            System.out.println("用户未购买,跳转购买");
            mav.addObject("url","buy/"+consumerCourse.getdata());
            mav.setViewName("learn/toBuyCourse");
            return mav;
        }
        System.out.println("用户已经购买,方法继续");

        //获取视频详情
        ServiceResponse videoInfo = videoService.getVideoByVideoId(videoId);

        //获取视频进度
        ServiceResponse rate = learningRateService.getRate(consumer.getConsumerId(), videoId);

//        if (videoInfo.getCode() == 200){
            mav.addObject("learnRate",rate.getdata());
            mav.addObject("videoInfo",videoInfo);
            mav.setViewName("learn/video");
//        }
        return mav;
    }
}
